package com.jamiexu.cn.chatwith.client;

import com.jamiexu.cn.chatwith.Imp.IConnectionListener;
import com.jamiexu.cn.chatwith.socket.ClientConnection;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.net.Socket;

public class ClientController {
    @FXML
    public TextField server_ip;

    @FXML
    public TextField server_port;

    @FXML
    public TextField server_nickname;

    @FXML
    public TextArea message;

    @FXML
    public TextField content;

    private ClientConnection clientConnection;

    private String nickname;

    @FXML
    public void server_connect() {
        String ip = server_ip.getText();
        String portString = server_port.getText();
        if (portString.equals("") || portString.length() == 0 || portString.matches("[^\\d]+") || Integer.parseInt(portString) > 65535) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示：");
            alert.setContentText("请输入1-65535之间的端口号");
            alert.show();
            return;
        }

        nickname = server_nickname.getText();
        if (ip.equals("")) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示：");
            alert.setContentText("请输入IP地址");
            alert.show();
            return;
        }

        if (nickname.equals("")) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示：");
            alert.setContentText("请输入昵称");
            alert.show();
            return;
        }

        new Thread(() -> {
            try {
                this.clientConnection = new ClientConnection(new Socket(ip, Integer.parseInt(portString)), nickname, new IConnectionListener() {
                    @Override
                    public void connected(String nickname) {
                        updateMessage("[系统消息]: " + nickname + "进入聊天室");
                    }

                    @Override
                    public void disConnected(String nickname) {
                        updateMessage("[系统消息]: " + nickname + "退出聊天室");
                    }

                    @Override
                    public void acceptMessage(String sender, String message) {
                        updateMessage("[" + sender + "]: " + message);
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


    }

    public void updateMessage(String msg) {
        Platform.runLater(() -> {
            this.message.setText(this.message.getText() + "\n" + msg);
        });
    }

    @FXML
    public void send() {
        String con = this.content.getText();
        if (con.equals(""))
            return;
        this.clientConnection.sendMessage(this.nickname, con);
//        this.message.setText(this.message.getText() + "\n[" + this.nickname + "]: " + con);
    }
}
